package docmodel;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 * 
 * @author giambattista
 *
 */
public class FloatingField {

	private String name;
	private String id;
	private String code;
	
	private Element span;
	private Element field;
	
	private String stroke;
	
	//private MarkerType type = MarkerType.FIELD;
	
	private static String patternLabel = "<#\\w*\\(.*,L\\)#>";
	private static Pattern pLabel = Pattern.compile(patternLabel);
	
	
	
	/**
	 * 
	 * @param marker
	 * @return
	 */
	public static boolean isLabel(String marker) {
		Matcher matcher = pLabel.matcher(marker);
		return matcher.matches();
	}
	

/**
 * 
 * @param fullMarkerCode
 */

	public FloatingField(String fullMarkerCode) {
		super();
		this.name = fullMarkerCode.substring(2, fullMarkerCode.indexOf("("));
		this.code = fullMarkerCode.substring(fullMarkerCode.indexOf("(")+1,fullMarkerCode.indexOf(",") );
		this.id= new String("FloatingField"+this.name+this.code);
		
		this.stroke = "lowered";
	}

	
	/**
	 * 
	 * @return
	 */
	public String getName()
	{
		return this.name;
	}

	/**
	 * 
	 * @return
	 */
	public String getId() {
		return id;
	}

	/**
	 * 
	 * @param id
	 */
	public void setId(String id) {
		this.id = id;
	}

	/**
	 * 
	 * @return
	 */
	public String getCode() {
		return code;
	}

	/**
	 * 
	 * @param code
	 */
	public void setCode(String code) {
		this.code = code;
	}

	/**
	 * 
	 * @param doc
	 * @return
	 */
	public Element getFieldElement(Document doc) {
        createFieldElement(doc);
        return this.field;
	}

	/**
	 * @param doc
	 */
	private void createFieldElement(Document doc) 
	{
		 Element ui, textEdit, border, edge;
        Element field = null;
               
        field = doc.createElement("field");
        
        //crea il figlio ui
        ui = doc.createElement("ui");
        
        //crea il figlio TextEdit
        textEdit = doc.createElement("textEdit");
        ui.appendChild(textEdit);
        
        //crea il figlio di UI: "border"
        border = doc.createElement("border");
        ui.appendChild(border);
        
        //crea il figlio di BORDER: "edge"
        edge = doc.createElement("edge");
        border.appendChild(edge);
        
        //attributi "edge"
        edge.setAttribute("stroke", this.stroke);
		
        //Aggiunge alcuni attributi
        field.setAttribute("name", this.name);
        field.setAttribute("id", this.id);
        field.appendChild(ui);
        this.field=field;
	}
	
	/**
	 * 
	 * @param doc
	 * @return
	 */
	public Element getSpanElement(Document doc) {
        createSpanElement(doc);
        return this.span;
	}

	/**
	 * @param doc
	 */
	private void createSpanElement(Document doc) {
		Element span;
        span = doc.createElement("span");
        span.setAttribute("xfa:embedType","uri");
        span.setAttribute("xfa:embedMode","raw");
        span.setAttribute("xfa:embed", "#"+this.id);
        this.span=span;
	}
	
	/**
	 * 
	 * @param args
	 */
	public static void main (String args[]) {
		String marker="<#ciao(123,L)#>";
		System.out.println(isLabel(marker)+" - "+ new FloatingField(marker).id);
		
		marker="<#ciao(123,F)#>";
		System.out.println(isLabel(marker)+" - "+ new FloatingField(marker).id);
	}
}
